﻿<%@ Page Title="Promotion Engine  -  Promotion Warehouse Exclusive" Language="C#" MasterPageFile="~/PromotionPage.Master" AutoEventWireup="true"
    CodeBehind="PromotionSalesPersonExclude.aspx.cs" Inherits="PromotionEngine.PromotionSalesPersonExclude" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPromotionPage" runat="server">
    <script type="text/javascript">
        var oPromotionSalesPersonExclude = {};
        var oSalesPerson;
        var isInsert = false;
        var gird;
        function wareHouseDropDownEditor(container, options) {
            $('<input required data-text-field="SalesPersonName" data-value-field="SalesPersonID" ID="SalesPersonID" data-bind="value:' + options.field + '"/>')
                        .appendTo(container)
                        .kendoDropDownList({
                            autoBind: false,
                            filter: "contains",
                            suggest: true,
                            optionLabel: "[Select Sales Person]",
                            dataSource: oSalesPerson
                        });
        }
        //Get reference
        function GetSalesPerson() {
            $.ajax({
                type: "GET",
                url: "SalesPerson.aspx/GetSalesPersonForManager",
                data: '',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    oSalesPerson = $.parseJSON(response.d);
                },
                failure: function (response) {
                    alert(response.d);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    if (jqXHR.status == 500) {
                        alert('Internal error: ' + jqXHR.responseText);
                    } else {
                        alert('Unexpected error.' + jqXHR.responseText);
                    }
                }
            });
        }

        $(document).ready(function () {
            GetSalesPerson();
            var promotionID = $('#<%=lblPromotionID.ClientID%>').val();
            var promotionCode = $('#<%=lblPromotionCode.ClientID%>').text();
            var promotionName = $('#<%=lblPromotionName.ClientID%>').text();
            $("#MasterPromotionCode").val(promotionCode);
            $("#MasterPromotionName").val(promotionName);
            $("#MasterPromotionID").val(promotionID);
            gird = $("#gridDetail").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            type: "GET",
                            url: "PromotionSalesPersonExclude.aspx/GetSalesPersonExclude",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        destroy: {
                            type: "GET",
                            url: "PromotionSalesPersonExclude.aspx/DeleteSalesPersonExclude",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        parameterMap: function (data, operation) {
                            if (operation === "read") {
                                return { promotionID: promotionID };
                            }
                            if (operation === "destroy") {
                                return { lineID: data.LineID };
                            }
                        },
                        error: function (a) {
                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true
                    },
                    type: "odata", //Important!!!!
                    schema: {
                        data: function (a) {
                            if (a.d != null) {
                                var data = a.d.Data;
                                return data;
                            }
                        },
                        total: "d.Count",
                        model: {
                            id: "LineID",
                            fields: {
                                LineID: { type: "number" },
                                PromotionID: { type: "string" },
                                SalesPersonName: { type: "string" },
                                SalesPersonID: { type: "string" }
                            }
                        }
                    }
                },

                sortable: true,
                pageable: {
                    pageSize: 20,
                    refresh: true
                },
                filterable: true,
                reorderable: true,
                resizable: true,
                height: 370,
                selectable: "row",
                toolbar: [{ name: 'create', text: 'Add New' }, { name: 'excel'}],
                excel: {
                    fileName: "Sales Person Exclusive List.xlsx",
                    filterable: true
                },
                batch: true,
                columns: [
                            { field: "LineID", title: "Property ID", width: 300, hidden: true },
                            { field: "PromotionID", title: "Property ID", width: 300, hidden: true },
                            { field: "SalesPersonID", title: "Sales Person", width: 400, editor: wareHouseDropDownEditor, template: "#=SalesPersonName#" },
                            { command: ["edit", "destroy"], title: "Action", width: "200px" }
                            ],
                editable: {
                    mode: "inline",
                    confirmation: "Are you sure you want to remove this data?"
                },
                edit: function (e) {
                    if (e.model.isNew()) {
                        isInsert = true;
                        $("a.k-grid-update")[0].innerHTML = "<span class='k-icon k-update'></span>Save";
                    }
                    else {
                        isInsert = false;
                    }
                },
                save: function (e) {
                    var listTextBox = $("input:text");
                    for (var i = 0; i < listTextBox.length; i++) {
                        var name = listTextBox[i].name;
                        if (name.length > 0) {
                            oPromotionSalesPersonExclude[name] = $("[name=" + name + "]").val();
                        }
                    }
                    oPromotionSalesPersonExclude.PromotionID = promotionID;
                    oPromotionSalesPersonExclude.SalesPersonID = $('#SalesPersonID').data("kendoDropDownList").value();
                    //Update or Insert
                    $.ajax({
                        type: "POST",
                        url: "PromotionSalesPersonExclude.aspx/UpdateSalesPersonExclude",
                        data: JSON.stringify({
                            oPromotionSalesPersonExclude: JSON.stringify(oPromotionSalesPersonExclude),
                            isInsert: isInsert
                        }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            //Reload Data 
                            gird.dataSource.read();
                            isInsert = false;
                        },
                        error: function (xhr, status, err) {
                            var err = eval("(" + xhr.responseText + ")");
                            alert(err.Message);
                        }
                    });
                    this.refresh();
                }
            }).data("kendoGrid");
        });
    </script>
    <h3>
        &nbsp;Sales Person <span class="k-icon k-i-arrowhead-e"></span>Exclusive
    </h3>
    <h2 style="display: none;">
        Promotion:&nbsp;
        <asp:Label ID="lblPromotionCode" runat="server"></asp:Label>
        -
        <asp:Label ID="lblPromotionName" runat="server"></asp:Label>
        <asp:HiddenField ID="lblPromotionID" runat="server"></asp:HiddenField>
    </h2>
    <div id="gridDetail">
    </div>
</asp:Content>
